package org.je.dev;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fanyifeng
 */
public class SynMap
{
	private int			arrLen;
	private InnerSynMap	innerMap;

	/**
	 * @param modLeft
	 */
	public SynMap(int arrLen) {
		innerMap = new InnerSynMap(arrLen);
		this.arrLen = innerMap.arrayLen;
	}

	public int size() {
		return innerMap.size();
	}

	public int arrayLength() {
		return arrLen;
	}
	
	public Object getLock(Object key){
		return innerMap.getLock(innerMap.pos(key));
	}

	/**
	 * @param key
	 * @param val
	 */
	public void put(Object key, Object val) {
		innerMap.put(key, val);
	}

	/**
	 * @param key
	 * @return val
	 */
	public Object get(Object key) {
		return innerMap.get(key);
	}

	/**
	 * @param key
	 * @return val
	 */
	public Object remove(Object key) {
		return innerMap.remove(key);
	}

	public void clear() {
		innerMap = new InnerSynMap(arrLen);
	}

	/**
	 * @param i
	 * @return list[k,v]
	 */
	public List<Object[]> scanArray(int i) {
		InnerSynMap mp = innerMap;
		if (mp.nodes[i] == null) {
			return new ArrayList<Object[]>(1);
		}
		List<Object[]> list = new ArrayList<Object[]>(2);
		synchronized(mp.getLock(i)) {
			InnerSynMap.Node nd = mp.nodes[i];
			while (nd != null) {
				// Sleep.millis(2000);
				list.add(new Object[] { nd.key, nd.val });
				nd = nd.next;
			}
		}
		return list;
	}

	public String getDetails() {
		return innerMap.getDetails();
	}
}
